quietly {

noisily: di	in yellow	"Just a moment please...{break}" " "

*-------------------------------------------------------------------------------*
* Structure of file
*
* 1. Prepare data set
* 2. Run estimations for each institution and legislative procedures
* 3. Prepare time series
* 4. Clean folder
*
* Version:	2016-06-29
* Changes:	2016-??-??: First release
*
* Notes for nerds: If you are not satisfied with our preselection of models
*	and want to add new estimates you need to carefully complement this syntax.
*	I have provided as many notes as reasonable from my point of view. If you 
*	have any question do not hesitate to contact me (duepont@uni-greifswald.de).
*-------------------------------------------------------------------------------*



* PREPARE DATA SET

* Generate smaller PIP to speed up estimation
use "datasets/pip_ts_aspm", clear
keep id g??? p??? $ideology
by g101, sort: egen eu_keep = mean(p601)
drop if eu_keep == .	// reduce to EU-member states
drop eu_keep p3?? p4??	// drop 1st and 2nd chamber data, because it is irrelevant


* Add already estimated government positions
gen _____GOVERNMENT_POSITION = .

clonevar techq = g105
clonevar iso = g101

merge iso techq using "$govposdata", sort nokeep uniqusing keep(GOV_POS)
drop _merge iso techq


* Add external data for estimating "power" of PMs
gen _____BARGAINING_POWER = .
merge g101 g103 using "datasets/external_data", sort nokeep uniqusing keep(*population* *gdp_cur)
drop _merge

save tmp_eu, replace



* RUN ESTIMATIONS
do techdata/est_eu_council.do		// Estimate Heads of States
do techdata/est_eu_commission.do	// Estimate Commission
do techdata/est_eu_councilofmin.do	// Estimate Council of Ministers
do techdata/est_eu_parliament.do	// Estimate European Parliament
do techdata/est_eu_legisprocess.do	// Apply Legislative Rules



* Merge with government data
use "$govposdata", clear
keep iso-eu _____ESTIMATES GOV_POS _____SELECTED_SPECS
merge m:1 techq eu using tmp_eu_legisprocess, keepusing(EUCOU_POS COMM_POS COUNCIL_POS EP_POS EU_POS ///
		european_council principal european_commission councilofministers european_parliament ) nogen



* Save quarterly data
local file $save
sort iso quarter
order EUCOU_POS COMM_POS COUNCIL_POS EP_POS EU_POS, after(GOV_POS)
notes replace _dta in 1: These are your ASPM Replication Estimates from TS
save `file'_quarterly, replace


* Save yearly data

	// Year 2004-mod, because the CEE countries became members of the EU on May 1, 2004.
	// For them the first quarter is "missing", which would distort the yearly mean, hence it is filled in.
foreach var of varlist EUCOU_POS COMM_POS COUNCIL_POS EP_POS EU_POS {

	gen  zs1_`var' = `var' if techq == 176
	egen zs2_`var' = max(zs1_`var')
	replace `var' = zs2_`var' if techq == 176 & (iso==196 | iso==203 | iso==233 | iso==348 | iso==428 | ///
												 iso==440 | iso==470 | iso==616 | iso==703 | iso==705)
	drop zs*
}
	// Year 2013-mod, because Croatia became a member of the EU on July 1, 2013.
	// For them the first quarter is "missing", which would distort the yearly mean, hence it is filled in.
foreach var of varlist EUCOU_POS COMM_POS COUNCIL_POS EP_POS EU_POS {

	gen  zs1_`var' = `var' if techq == 212
	egen zs2_`var' = max(zs1_`var')
	gen  zs3_`var' = `var' if techq == 213
	egen zs4_`var' = max(zs3_`var')
	replace `var' = zs2_`var' if techq == 212 & iso==191
	replace `var' = zs4_`var' if techq == 213 & iso==191
	drop zs*
}
	// Aggregate
foreach var of varlist GOV_POS EUCOU_POS COMM_POS COUNCIL_POS EP_POS EU_POS eu {

	clonevar z`var' = `var'
	by iso year, sort: egen z`var'_zs=mean(`var')
	replace z`var' = z`var'_zs
	drop z`var'_zs `var'
	capture: replace eu = 1 if eu>=0 & eu!=.
	
}
rename z* *
by iso year, sort: keep if _n == 4
drop quarter techq
order _____SELECTED_SPECS european_council principal european_commission councilofministers european_parliament, last
order eu, after(year)
save `file'_yearly, replace



* Erase or keep the tmp files for inspection
if "$files" == "erase" {
	erase tmp_eu_council.dta
	erase tmp_commission.dta
	erase tmp_councilofmin.dta
	erase tmp_euparliament.dta
	erase tmp_eu_legisprocess.dta
}
erase tmp_eu.dta



* Output
noisily: di	in yellow	"You are done! Have good results with the PIP data.{break}"	///
				"Type " in green "quarterly" in yellow " or " in green "yearly" in yellow " to browse the corresponding time-series"	_request(ans)
local usef1 $save
local usef2 $ans
use `usef1'_`usef2', clear
browse
} 	// close quietly bracket
